package cn.itcast.nems.studentrecord.service.impl.studentrecord;

import cn.itcast.nems.studentrecord.dao.entity.StudentRecordReceivable;
import cn.itcast.nems.studentrecord.dao.mapper.StudentRecordReceivableMapper;
import cn.itcast.nems.studentrecord.dto.studentrecord.StudentRecordReceivableCreateDTO;
import cn.itcast.nems.studentrecord.service.studentrecord.StudentRecordReceivableService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 应收明细;所有对学生的收款都走应收单 服务实现类
 * </p>
 *
 * @author liyong
 * @since 2023-11-30
 */
@Service
@AllArgsConstructor
public class StudentRecordReceivableServiceImpl implements StudentRecordReceivableService {
    private final StudentRecordReceivableMapper studentRecordReceivableMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long addStudentRecordReceivable(StudentRecordReceivableCreateDTO createDTO) {
        final Long id = this.studentRecordReceivableMapper.selectId(createDTO.getStudentRecordId(),
                createDTO.getReferenceType().name(),
                createDTO.getReferenceId(),
                createDTO.getReferenceLineId());
        if (id != null) {
            return id;
        }
        StudentRecordReceivable entity = new StudentRecordReceivable();
        BeanUtils.copyProperties(createDTO, entity);
        entity.setReferenceType(createDTO.getReferenceType().name());
        this.studentRecordReceivableMapper.insert(entity);
        return entity.getId();
    }

}
